<?php
namespace api\util;

class Time
{

    public static function getDate($timestamp, $type = 0)
    {
        $now  = time();
        $diff = $now - $timestamp;

        if ($diff / 86400 > 30)
        {
            return intval($diff / 86400) . '天';
        }
        //
        if ($diff <= 60)
        {
            return '刚刚';
        }
        elseif ($diff <= 3600)
        {
            return floor($diff / 60) . '分钟前';
        }
        elseif ($diff <= 86400)
        {
            return floor($diff / 3600) . '小时前';
        }
        elseif ($diff <= 2592000 || $type = 1)
        {
            return floor($diff / 86400) . '天前';
        }
        else
        {
            return '一个月前';
        }
    }

    public static function getWeek()
    {

        $sdefaultDate = date("Y-m-d");
        //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
        $first = 1;
        //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
        $w = date('w', strtotime($sdefaultDate));

        //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
        $week_start = date('Y-m-d', strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days'));
        $day        = 6;
        $date       = [];

        for ($i = 0; $i <= $day; $i++)
        {
            $date[] = date('Y-m-d', strtotime("$week_start +{$i} days"));
        }
        return $date;
    }

    //获取月份时间
    public static function getMonth($date = 0)
    {

		$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
		$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
		$day = ceil(($endThismonth-$beginThismonth)/86400);

		$monthStart = date('Y-m-d',$beginThismonth);

        $date       = [];
		for ($i = 0; $i <= $day-1; $i++)
        {
            $date[] = date('Y-m-d', strtotime("$monthStart +{$i} days"));
        }
        return $date;

    }


    /**
     * @description:根据数据
     * @param {dataArr:需要分组的数据；keyStr:分组依据}
     * @return:
     */
    // public static function dataGroup(array $dataArr, string $keyStr): array
    // {
    //     $newArr = [];
    //     foreach ($dataArr as $k => $val)
    //     {

    //         $newArr[$val[$keyStr]][] = $val;
    //     }
    //     return $newArr;
    // }

}
